//
//  ScenicNodeVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/12/28.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "ScenicNodeVC.h"
#import "ScenicNodeCCell.h"
#import "ScenicDescribeVC.h"

#define pagesize  20
@interface ScenicNodeVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UICollectionView *CollectionView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic) NSInteger pageno;
@end

@implementation ScenicNodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
 
    self.title = MYLocalizedString(@"HOMEPAGE_SITE", nil);
    
    [self setup];
}

- (void)setup {
    
    
    [self.CollectionView registerNib:[UINib nibWithNibName:@"ScenicNodeCCell" bundle:nil] forCellWithReuseIdentifier:@"ScenicNodeCCell"];
    
    self.view.backgroundColor = kBGColor;
    self.CollectionView.backgroundColor = kBGColor;
    @weakify(self);
    self.CollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        self.pageno = 0;
        [self.CollectionView.mj_footer resetNoMoreData];
        [self loadDataSource];
    }];
    
    self.CollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        @strongify(self);
        [self loadDataSource];
    }];
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    [self loadDataSource];
    [self.CollectionView setThemeStyle:ThemeStyleBlack];
}

- (void)loadDataSource {
    
    @weakify(self);
    [NetAPIManager  getAllViewsWithCity:self.city.cid start:[NSNumber numberWithInteger:_pageno] count:@(pagesize) block:^(NSArray *viewsArray, NSError *error) {
        @strongify(self);
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        // [self.imageView setImageURL:[NSURL URLWithString:city.pictures] placeholderImage:[UIImage imageNamed:@"city.jpeg"]];
        
        if (viewsArray.count < 20) {
            [self.CollectionView.mj_footer endRefreshingWithNoMoreData];
        } else {
            
            [self.CollectionView.mj_footer endRefreshing];
        }
        if (self.pageno == 0) {
            self.dataSource =  [NSMutableArray arrayWithArray:viewsArray];
            [self.CollectionView.mj_header endRefreshing];
            
            if (viewsArray.count < 20) {
                self.CollectionView.mj_footer = nil;
            } else {
                self.CollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                    @strongify(self);
                    [self loadDataSource];
                }];
            }
        }else {
            [self.dataSource addObjectsFromArray:viewsArray];
            [self.CollectionView.mj_footer endRefreshing];
        }
        
        if (viewsArray.count < pagesize) {
            [self.CollectionView.mj_footer endRefreshingWithNoMoreData];
        }
        self.pageno ++;
        [self.CollectionView reloadData];
        
    }];
}





#pragma mark     -- UICollectionViewDelegate,UICollectionViewDataSource



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    ScenicNodeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ScenicNodeCCell" forIndexPath:indexPath];
    
    Scenic * scenic = self.dataSource[indexPath.row];
    
    [cell configDataWith:scenic];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat width = (kScreenWidth - 42)/2;
    CGFloat height = width;
    
    return CGSizeMake(width, height);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ScenicDescribeVC *scenicDesVC = [ScenicDescribeVC new];
    Scenic *scenic = self.dataSource[indexPath.row];
    scenicDesVC.scenicid = scenic.ID;
    [self.navigationController pushViewController:scenicDesVC animated:YES];
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    
    [MobClick event:@"5_1_2"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
